///**
// *
// */
//package com.kgcx.schedule.client;
//
//import java.util.function.Function;
//import java.util.function.Supplier;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import com.kgcx.common.bean.ReturnData;
//import com.kgcx.core.exception.BusinessException;
//import com.netflix.client.ClientException;
//
//import feign.RetryableException;
//
///**
// * @author 袁进勇
// *
// */
//public class FeignUtil {
//    private static final Logger LOG = LoggerFactory.getLogger(FeignUtil.class);
//
//    public static <T> T invoke(Supplier<T> supplier) {
//        return invoke(supplier, (result) -> {
//            LOG.error("服务接口调用返回失败！code=" + result.getCode() + "，msg=" + result.getMsg());
//            throw new BusinessException(16102899996L,
//                    "服务接口调用返回失败！code=" + result.getCode() + "，msg=" + result.getMsg());
//        });
//    }
//
//    public static <T> T invoke(Supplier<T> supplier, Function<ReturnData<T>, T> failedCallback) {
//        return invoke(supplier, failedCallback, (e) -> {
//            if (e instanceof RetryableException) {
//                // Connection refused: connect executing GET http://organization/city/getCity?kid=4301
//                String msg = e.getMessage();
//                Pattern p = Pattern.compile("^Connection refused: connect executing (.*)$");
//                Matcher m = p.matcher(msg);
//                if (m.find()) {
//                    msg = "服务[" + m.group(1) + "]网络连接拒绝！";
//                }
//
//                LOG.error(msg, e);
//                throw new BusinessException(16102899997L, msg, e);
//            }
//
//            Throwable t = e.getCause();
//            if (t instanceof ClientException) {
//                // Load balancer does not have available server for client: organization
//                String msg = t.getMessage();
//                Pattern p = Pattern.compile("^Load balancer does not have available server for client: (.*)$");
//                Matcher m = p.matcher(msg);
//                if (m.find()) {
//                    msg = "服务[" + m.group(1) + "]未启动！";
//                }
//
//                LOG.error(msg, e);
//                throw new BusinessException(16102899998L, msg, t);
//            }
//
//            LOG.error("其他异常！", e);
//            throw new BusinessException(16102899999L, "其他异常！" + e.getMessage(), e);
//        });
//    }
//
//    public static <T> T invoke(Supplier<ReturnData<T>> supplier, Function<ReturnData<T>, T> failedCallback,
//            Function<Exception, T> exceptionCallback) {
//        try {
//            ReturnData<T> result = supplier.get();
//            if (result.success()) {
//                return result.getData();
//            }
//
//            if (failedCallback == null) {
//                LOG.warn("服务接口调用返回失败！code=" + result.getCode() + "，msg=" + result.getMsg());
//                return null;
//            }
//
//            return failedCallback.apply(result);
//        } catch (Exception e) {
//            if (exceptionCallback == null) {
//                LOG.warn("服务接口调用异常！", e);
//                return null;
//            }
//
//            return exceptionCallback.apply(e);
//        }
//    }
//}
